#pragma once
#include<vector>
#include<list>
#include<queue>
#include<iostream>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

using namespace std;
namespace hu
{
	template<class T, class container = vector<T>, class Compare = less<T>>
	class priority_queue
	{
	public:
		void Adjustup()
		{
			int child = heap.size() - 1;
			int parent = (child - 1) / 2;
			while (parent >=0)
				if (com(heap[parent], heap[child]))
				{
					std::swap(heap[parent], heap[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
					
		}

		void Adjustdown(int parent)
		{
			int child = parent * 2 + 1;
			if (com(heap[child], heap[child + 1]))
			{
				child++;
			}
			while (child < heap.size())
			{
				if (com(heap[parent], heap[child]))
				{
					swap(heap[parent], heap[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}
		priority_queue()
		{}
		template<class inputiterator>
		priority_queue(inputiterator first, inputiterator last)
		{
			auto it = first;
			while (it != last)
			{
				push(*it);
				it++;
			}
		}
		void push(const T& x)
		{
			heap.push_back(x);
			Adjustup();
		}

		void pop()
		{
			swap(heap[0], heap[heap.size() - 1]);
			heap.pop_back();
			Adjustdown(0);
		}


		const int size() const
		{
			return heap.size();
		}

		bool empty() const
		{
			return heap.size() == 0;
		}
		const T& top()
		{
			return heap[0];
		}

	private:
		container heap;
		Compare com;
	};
}